import os
import sys

from core.result_base import ResultBase
from api.user import user
from common.logger import logger
from common.content_type import ContentType
from common.user_agent import useragent
from common.time_bdp import time_bdp
import urllib3
urllib3.disable_warnings()

BASE_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(BASE_PATH)


def user_login(domain,username,password,_xsrf={}):
    """
    用户登录
    :param domain: 企业登录域
    :param username: 用户名
    :param password : 密码
    :param _xsrf :
    :return: 自定义的关键字返回结果 result
    """
    result = ResultBase()
    payload = {
        "domain": domain,
        "username": username,
        "password": password,
        "_xsrf":_xsrf
    }

    headers = {
        "Content-Type": ContentType.MULTIPART_FORM_DATA.value,
        "User-Agent": useragent.get_user_agent_one()
    }

    res = user.login(params=payload, headers=headers)
    result.success = False
    if res.json()["status"] == "0":
        result.success = True
        result.access_token = res.json()["result"]["access_token"]
    else:
        result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
    result.payload = res.json()
    result.response = res
    logger.info("用户登录 ==>> 返回结果 ==>> {}".format(result.response.text))
    return result


def user_info(access_token):
    """
    获取用户信息
    :param access_token: access_token
    :return: 自定义的关键字返回结果 result
    """
    result = ResultBase()

    payload = {

    }

    headers = {
        "Content-Type":ContentType.APPLICATION_JSON.value,
        "User-Agent":useragent.get_user_agent_one()
    }

    cookies = {
        "access_token":access_token
    }
    res = user.info(params=payload,headers=headers,cookies=cookies)
    result.success = False
    if res.json()["status"] == "0":
        result.success = True
    else:
        result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
    result.payload = res.json()
    result.response = res
    logger.info("获取用户信息 ==>> 返回结果 ==>> {}".format(result.response.text))
    return result



def user_logout(access_token,_xsrf=""):
    """
    用户登出
    :param access_token: access_token
    :param _t: 13位的时间戳
    :param _xsrf :默认传空
    :return: 自定义的关键字返回结果 result
    """
    result = ResultBase()
    payload = {
        "_t": time_bdp.time_stamp_13(),
        "_xsrf": _xsrf
    }

    headers = {
        "Content-Type": ContentType.MULTIPART_FORM_DATA.value,
        "User-Agent":useragent.chrome()
    }

    res = user.logout(access_token,data=payload, headers=headers)
    result.success = False
    if res.json()["status"] == "0":
        result.success = True
        result.sessionId = res.json()["result"]["access_token"]
    else:
        result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
    result.payload = res.json()
    result.response = res
    logger.info("用户登出 ==>> 返回结果 ==>> {}".format(result.response.text))
    return result



domain = "haizhi"
username = "admin"
password = "admin"
_xsrf = {}
#
result = user_login(domain,username,password,_xsrf)

print(result.access_token)
access_token = result.access_token


user_info(access_token)

